package com.util;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;

import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * 基础加密组件
 * 
 * @author WANGYONG
 * @version 1.0
 * @since 2013-01-24
 */
public class Coder {
	public static final String KEY_SHA = "SHA";
	public static final String KEY_MD5 = "MD5";

	/**
	 * MAC算法可选以下多种算法
	 * 
	 * <pre>
	 * HmacMD5 
	 * HmacSHA1 
	 * HmacSHA256 
	 * HmacSHA384 
	 * HmacSHA512
	 * </pre>
	 */
	public static final String KEY_MAC = "HmacMD5";

	/**
	 * BASE64解密
	 * 
	 * @param key
	 * @return
	 * @throws Exception
	 */
	public static byte[] decryptBASE64(String key) throws Exception {
		return (new BASE64Decoder()).decodeBuffer(key);
	}

	/**
	 * BASE64加密
	 * 
	 * @param key
	 * @return
	 * @throws Exception
	 */
	public static String encryptBASE64(byte[] key) throws Exception {
		return (new BASE64Encoder()).encodeBuffer(key);
	}

	/**
	 * MD5加密
	 * 
	 * @param data
	 * @return
	 * @throws Exception
	 */
	public static byte[] encryptMD5(byte[] data) throws Exception {

		MessageDigest md5 = MessageDigest.getInstance(KEY_MD5);
		md5.update(data);

		return md5.digest();

	}

	/**
	 * SHA加密
	 * 
	 * @param data
	 * @return
	 * @throws Exception
	 */
	public static byte[] encryptSHA(byte[] data) throws Exception {

		MessageDigest sha = MessageDigest.getInstance(KEY_SHA);
		sha.update(data);

		return sha.digest();

	}

	/**
	 * 初始化HMAC密钥
	 * 
	 * @return
	 * @throws Exception
	 */
	public static String initMacKey() throws Exception {
		KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_MAC);

		SecretKey secretKey = keyGenerator.generateKey();
		return encryptBASE64(secretKey.getEncoded());
	}

	/**
	 * HMAC加密
	 * 
	 * @param data
	 * @param key
	 * @return
	 * @throws Exception
	 */
	public static byte[] encryptHMAC(byte[] data, String key) throws Exception {

		SecretKey secretKey = new SecretKeySpec(decryptBASE64(key), KEY_MAC);
		Mac mac = Mac.getInstance(secretKey.getAlgorithm());
		mac.init(secretKey);

		return mac.doFinal(data);

	}
	
	/**
	 * 密码加密方法 
	 * @param inputStr 密码字符串
	 * @return  加密串
	 * @throws Exception
	 */
	public static String pwdEncryption(String inputStr) throws Exception{
		byte[] inputData = inputStr.getBytes();
		inputStr =encryptBASE64(inputData);
		inputData = inputStr.getBytes();
		String key = "www.meal.com";
		inputData = encryptHMAC(inputData, key);
		BigInteger md5 = new BigInteger(encryptMD5(inputData));
		
		return md5.toString(32);
	}
	/**
	 * 获取唯一码
	 * @return
	 * @throws InterruptedException
	 */
	public static String getUniqueCode() {
        String str = "0123456789abcdefghijklmnopqrstuvwxyz";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM-ddHHmm-ssms");
    	String ly_time = sdf.format(new java.util.Date());
    	int bStrInt = 20-ly_time.length();
    	for (int i = 0; i < bStrInt; i++) {
    		ly_time=ly_time+"0";
		}
        StringBuilder builder = new StringBuilder();
        for (int i = 1; i <= 7; i++) {
        	if(i%7==0){
        		builder.append("-");
        	}else{
        		builder.append(str.charAt((int) (Math.random() * 36)));
        	}
        }
        builder.append(ly_time);
        return builder.toString().toUpperCase();
    }
	
	public static void main(String[] args) throws Exception {
		System.out.println(Coder.getUniqueCode());
		//System.out.println(Coder.pwdEncryption("123456"));
		
	}
	
}

